package com.jt.trffic.balance.mq;

import com.google.gson.Gson;
import com.jt.trffic.balance.entity.DsVehPass;
import com.jt.trffic.balance.utils.RedisUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;

/**
 * @Description: 违法数据消费，然后写入到webservice
 * @author: jxx
 * @Date:
 */
@Component
public class PassDataMq {
    //@Autowired
    //VehService vehService;
    // @Resource
    // private AutoVehicleRecordMapper autoVehicleRecordMapper;
    //
    private static Logger logger = LoggerFactory.getLogger(PassDataMq.class);
    private RedisUtils redisUtils;

    @RabbitListener(admin = "BchtAdmin",
            containerFactory = "simpleRabbitListenerContainerFactory",
            bindings = {@QueueBinding(
                    value = @Queue(value = "${vehpass.data.mq.queue}", durable = "true", autoDelete = "false", exclusive = "false")
                    , exchange = @Exchange(value = "${vehpass.data.mq.exchange}", type = ExchangeTypes.TOPIC, durable = "true", autoDelete = "false")
                    , key = "${vehpass.data.mq.routingkey}")})
    private void rabbitHandler(Message message) {
        DsVehPass dsVehPass = null;
        try {
            dsVehPass = toBean(message, DsVehPass.class);
            //过车数据从my取出

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    <T> T toBean(Message message, Class<T> classOfT) throws UnsupportedEncodingException {
        String json = null;
        json = new String(message.getBody(), "UTF-8");
        return new Gson().fromJson(json, classOfT);


    }
}
